<!--
  -    Copyright (c) 2018-2025, lengleng All rights reserved.
  -
  - Redistribution and use in source and binary forms, with or without
  - modification, are permitted provided that the following conditions are met:
  -
  - Redistributions of source code must retain the above copyright notice,
  - this list of conditions and the following disclaimer.
  - Redistributions in binary form must reproduce the above copyright
  - notice, this list of conditions and the following disclaimer in the
  - documentation and/or other materials provided with the distribution.
  - Neither the name of the pig4cloud.com developer nor the names of its
  - contributors may be used to endorse or promote products derived from
  - this software without specific prior written permission.
  - Author: lengleng (wangiegie@gmail.com)
  -->
<template>
  <div class="execution">
    <basic-container>
      <avue-crud
        ref="crud"
        :page.sync="page"
        :search.sync="searchForm"
        :data="tableData"
        :permission="permissionList"
        :table-loading="tableLoading"
        :option="tableOption"
        :expandRowKeys="expandRowKeys"
        @on-load="getList"
        @search-change="searchChange"
        @refresh-change="refreshChange"
        @size-change="sizeChange"
        @current-change="currentChange"
        @row-update="handleUpdate"
        @row-save="handleSave"
        @row-del="rowDel"
      >
        <template slot-scope="scope" slot="studentStatusSearch">
          <el-checkbox
            v-model="searchForm.studentStatus"
            true-label="01"
            :false-label="null"
            checked
            >仅列出在业生</el-checkbox
          >
        </template>
        <template slot="studentName" slot-scope="scope">
          {{ scope.row.studentName }}
          <span v-if="scope.row.studentOldName">
            ( 曾用名 : {{ scope.row.studentOldName }} )
          </span>
        </template>
        <template slot-scope="scope" slot="menuLeft">
          <el-button size="small" type="primary">调班</el-button>
        </template>
        <template slot="expand" slot-scope="{ row }">
          <table border="0" cellspacing="0" cellpadding="0">
            <tr>
              <td width="80">姓名</td>
              <td width="80">{{ row.studentName }}</td>
              <td width="80">曾用名</td>
              <td width="80">&nbsp;</td>
              <td width="160" rowspan="6"><img :src="row.studentPhoto" /></td>
            </tr>
            <tr>
              <td>性别</td>
              <td>{{ row.studentSex }}</td>
              <td>出生年月</td>
              <td>{{ row.studentBirthday }}</td>
            </tr>
            <tr>
              <td>民族</td>
              <td>&nbsp;</td>
              <td>健康状况</td>
              <td>&nbsp;</td>
            </tr>
            <tr>
              <td>籍贯</td>
              <td colspan="3">&nbsp;</td>
            </tr>
            <tr>
              <td>身份证号</td>
              <td colspan="3">{{ row.studentIdCode }}</td>
            </tr>
            <tr>
              <td>学籍号</td>
              <td colspan="3">&nbsp;</td>
            </tr>
            <tr>
              <td colspan="2">最感兴趣的职业/行业/专业</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
            </tr>
            <tr>
              <td>政治面貌</td>
              <td colspan="2">&nbsp;</td>
              <td>爱好特长</td>
              <td>&nbsp;</td>
            </tr>
            <tr>
              <td>通信地址</td>
              <td colspan="4">{{ row.studentAddress }}</td>
            </tr>
            <tr>
              <td>邮政编码</td>
              <td colspan="2">{{ row.studentPostcode }}</td>
              <td>联系电话</td>
              <td>{{ row.studentPostCode }}</td>
            </tr>
            <tr>
              <td rowspan="5">
                <p>家庭成员</p>
                <p>相关信息</p>
              </td>
              <td>称谓</td>
              <td>姓名</td>
              <td>工作单位</td>
              <td>联系电话</td>
            </tr>
            <tr>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
            </tr>
            <tr>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
            </tr>
            <tr>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
            </tr>
            <tr>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
            </tr>
          </table>
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import { fetchList, getObj, addObj, putObj, delObj } from "./api";
import { tableOption } from "./crud";
import { mapGetters } from "vuex";

export default {
  name: "student",
  data() {
    return {
      searchForm: {},
      tableData: [],
      expandRowKeys: [],
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 20, // 每页显示多少条
      },
      tableLoading: false,
      tableOption: tableOption,
    };
  },
  computed: {
    ...mapGetters([
      "permission",
      "homePage",
      "schoolId",
      "clazzGrade",
      "clazzId",
    ]),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.code_student_add, false),
        delBtn: this.vaildData(this.permission.code_student_del, false),
        editBtn: this.vaildData(this.permission.code_student_edit, false),
      };
    },
  },
  watch: {
    deep: true, //true 深度监听
    homePage: {
      handler(val, oldVal) {
        this.refreshChange();
      },
    },
  },
  methods: {
    getList(page, params, mn) {
      this.tableLoading = true;
      fetchList(
        Object.assign(
          {
            current: page.currentPage,
            size: page.pageSize,
            studentClazz: this.clazzId,
            studentSchool: this.schoolId,
            studentGrade: this.clazzGrade,
          },
          params,
          this.searchForm
        )
      )
        .then((response) => {
          this.tableData = response.data.data.records;
          this.page.total = response.data.data.total;
          this.tableLoading = false;
        })
        .catch(() => {
          this.tableLoading = false;
        });
    },
    rowDel: function(row, index) {
      this.$confirm("是否确认删除ID为" + row.studentId, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function() {
          return delObj(row.studentId);
        })
        .then((data) => {
          this.$message.success("删除成功");
          this.getList(this.page);
        });
    },
    handleUpdate: function(row, index, done, loading) {
      putObj(row)
        .then((data) => {
          this.$message.success("修改成功");
          done();
          this.getList(this.page);
        })
        .catch(() => {
          loading();
        });
    },
    handleSave: function(row, done, loading) {
      addObj(row)
        .then((data) => {
          this.$message.success("添加成功");
          done();
          this.getList(this.page);
        })
        .catch(() => {
          loading();
        });
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    currentChange(current) {
      this.page.currentPage = current;
    },
    searchChange(form, done) {
      this.searchForm = form;
      this.page.currentPage = 1;
      this.getList(this.page, form);
      done();
    },
    refreshChange() {
      this.getList(this.page);
    },
  },
};
</script>
